package app.web.galgame.cgs.error;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import app.client.chs.PinYinUtils;
import app.esb.common.FileTools;
import app.esb.common.NumberTools;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.lang.Basic;
import cn.lily.com.lang.BusinessDate;
import cn.lily.com.lang.number.MathUtil;
/**
 * 明细分析
 * @author JiaJ
 *
 */
public class GalAutoErrorDetail {
	
	private String fileName;
	
	private String fileDate;
	
	private File folder;
	
	private String gameName;
	
	private String gameID;
	
	private String gameType = "ADV";
	
	private String gameLang = "02";
	
	private String gameVisual = "01";
	
	private String gameSize = "";
	
	private String gameFactory = "";
	
	private String gameMemo = "";
	
	private String gameDate = "";
	
	private String gameNamePlus = "";
	
	private String diskID = "H3";
	
	private String gameStorePath = "";
	
	private String gameTags = "";
	
	private Set<String> gameNameSet = new HashSet<String>();
	
	private Set<String> gameTagSet = new HashSet<String>();

	public void setFileName(String fileName) {
		this.fileName = this.getString(fileName);
	}

	public void setFileDate(String fileDate) {
		this.fileDate = fileDate;
	}

	public void setFolder(File folder) {
		this.folder = folder;
	}
	
	public void analyse(){
		
		try {
			
			System.out.println("【分析】"+fileName);
			
			if(this.analyseOringnalCG()){
				
				//存在匹配数据
				if(!this.analyseOringnalGal()){
					
					this.analyseData();
					
					this.insertData();
					
					this.moveFile();
					
				}
				
				//未匹配成功
				else{
					
					/*this.removeToError();*/
					
				}
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private boolean analyseOringnalCG(){
		
		String dateF = BusinessDate.formatDate(this.fileDate);
		
		String sqlString = "select RES_ID,GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,"
								+ "GAME_VISUAL,GAME_SIZE,GAME_SIZE_NUM,GAME_FACTORY,GAME_FIRST,"
								+ "GAME_INTRU,GEME_MEMO,GAME_NAME_PLUS,"
								+ "DISK_ID,GAME_STORE_PATH,GAME_TAGS,"
								+ "to_char(GAME_PRO_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_PRO_TIME,"
								+ "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME "
						 + "from ${schema}.LEO_CG_INFO "
						 + "where ( upper(GAME_NAME) like '" + Basic.toWhereLike(fileName.trim().toUpperCase()) + "' or upper(GAME_NAME_PLUS) like '" + Basic.toWhereLike(fileName.trim().toUpperCase()) + "')";
		
		DataTable orignalTable = DBFactory.getThreadDB().executeQueryTable(sqlString);
		
		while(orignalTable.next()){
			
			String activeDate = orignalTable.getString("GAME_PRO_TIME");
			
			if(activeDate.length() > 10){
				activeDate = activeDate.substring(0, 10);
			}
			
			if(dateF.equals(activeDate)){
				
				double gameSizeStore = NumberTools.toKBSize(orignalTable.getString("GAME_SIZE"));
				double gameSizeActive = NumberTools.toKBSize(FileTools.getFileSize(folder.getPath().replaceAll("\\\\", "/")));
				
				if(gameSizeActive > gameSizeStore){
					this.removeToBig();
				}else{
					this.removeToExist();
				}
					
				return false;
			}
			
		}
		
		return true;
		
	}
	
	private boolean analyseOringnalGal(){
		
		String sqlString = "select RES_ID,GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,"
								+ "GAME_VISUAL,GAME_SIZE,GAME_SIZE_NUM,GAME_FACTORY,GAME_FIRST,"
								+ "GAME_INTRU,GEME_MEMO,GAME_NAME_PLUS,"
								+ "DISK_ID,GAME_STORE_PATH,GAME_TAGS,"
								+ "to_char(GAME_PRO_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_PRO_TIME,"
								+ "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME "
						 + "from ${schema}.LEO_GALGAME_INFO_JPS "
						 + "where ( upper(GAME_NAME) like '" + Basic.toWhereLike(fileName.trim().toUpperCase()) + "' or upper(GAME_NAME_PLUS) like '" + Basic.toWhereLike(fileName.trim().toUpperCase()) + "')";
		
		DataTable orignalTable = DBFactory.getThreadDB().executeQueryTable(sqlString);
		
		if(orignalTable.next()){
				
			this.gameMemo = orignalTable.getString("GAME_INTRU");
			this.gameFactory = orignalTable.getString("GAME_FACTORY");
			this.gameNamePlus = orignalTable.getString("GAME_NAME_PLUS");
			this.gameTags = orignalTable.getString("GAME_TAGS");
			this.gameType = orignalTable.getString("GAME_TYP");
			
			return true;
			
		}
		
		return false;
		
	}
	
	
	
	private void analyseData(){
		
		System.out.println("【数据分析】"+fileName);
		
		this.gameID = System.currentTimeMillis() + MathUtil.random(3);
		this.gameDate = BusinessDate.formatDate(fileDate);
		this.gameName = this.fileName;
		this.gameSize = FileTools.getFileSize(folder.getPath().replaceAll("\\\\", "/"));
		if(this.gameMemo == null){
			this.gameMemo = "未知";
		}
		if(this.gameFactory == null){
			this.gameFactory = "";
		}
		if(this.gameNamePlus == null){
			this.gameNamePlus = "";
		}
		if(this.gameTags == null){
			this.gameTags = "未知";
		}
		if(this.gameType == null){
			this.gameType = "ADV";
		}

		this.gameStorePath = "/"+this.fileDate.substring(0,4) + "年/" + this.fileName;
		
	}
	
	private void insertData(){
		
		System.out.println("【数据存储】"+fileName);
		
		String firstWd = PinYinUtils.getFirstWord(this.fileName);
		
		String insertSQL = "insert into ${schema}.LEO_CG_INFO "
				+ "(GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,GAME_VISUAL,"
				+ "GAME_SIZE,GAME_FACTORY,GAME_INTRU,"
				+ "GAME_PRO_TIME,GAME_CRE_TIME,"
				+ "GAME_NAME_PLUS,DISK_ID,GAME_STORE_PATH,GAME_TAGS,GAME_FIRST) "
				+ "values "
				+ "('"+gameID+"','"+this.gameName+"','"+this.gameType+"','"+this.gameLang+"','"+this.gameVisual+"',"
				 + "'"+this.gameSize+"','"+this.gameFactory+"','"+this.gameMemo+"',"
				 + "to_date('"+this.gameDate+"','yyyy-mm-dd'),to_date('"+BusinessDate.getTodaytime()+"','yyyy-mm-dd hh24:mi:ss'),"
				 + "'"+this.gameNamePlus+"','"+this.diskID+"','"+this.gameStorePath+"','"+this.gameTags+"','"+firstWd+"') ";
		
		DBFactory.getThreadDB().executeUpdate(insertSQL);
		
	}
	
	private void moveFile(){
		File file = new File("H:/"+this.fileDate.substring(0,4) + "年");
		if(!file.exists()){
			file.mkdirs();
		}
		
		this.folder.renameTo(new File("H:/"+this.fileDate.substring(0,4) + "年/"+this.folder.getName()));
	}
	
	protected String getString(String src){
		
		if(src == null){
			return "";
		}
		
		return src.replaceAll("'", "’")
				  .replaceAll("\"", "”")
				  .replaceAll("&nbsp;", "")
				  .replaceAll("<br>", "")
				  .replaceAll("<br/>", "")
				  .replaceAll("<p>", "")
				  .replaceAll("</p>", "");
	}
	
	public void removeToExist(){
		this.folder.renameTo(new File("H:/待处理/ANLYSE/Exist/"+this.folder.getName()));
	}
	
	public void removeToBig(){
		this.folder.renameTo(new File("H:/待处理/ANLYSE/Big/"+this.folder.getName()));
	}
	
	public void removeToError(){
		this.folder.renameTo(new File("H:/待处理/ANLYSE/Error/"+this.folder.getName()));
	}
	
}
